import { Component } from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {AppGlobal, AppService} from '../../app/app.service';
import {LoginPage} from '../login/login'

/**
 * Generated class for the RepositoryAddPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-repository-add',
  templateUrl: 'repository-add.html',
})
export class RepositoryAddPage {

  topicParams: any;
  repositorytype:any;
  user:any;
  loginsts:any;
  showflg: boolean = false;
  errormsg: any = "账号输入有误！";

  params = {
    pageNo: 1,
    pageSize: 20
  };

  constructor(public navParams: NavParams,public appService: AppService,public navCtrl: NavController) {
    //登录后操作
    this.loginsts = localStorage.getItem('loginsts');
    //电脑测试
    //this.loginsts = "1";
    if (!(this.loginsts == "1")){
      this.navCtrl.push(LoginPage);
    }
    //获得选择框参数
    this.Repositorytype();
    //选择框默认值
    let typeid = this.navParams.get("item");
    //知识库参数初始化
    this.topicParams = {
      cause: '',
      typeid: typeid?typeid:'',
      title: '',
      resolvent: '',
      createuser:'',
      typename:'',
      sessiontoken:''
    }
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad RepositoryAddPage');
  }
  //获得知识库类型
  Repositorytype() {
    this.appService.httpGet(AppGlobal.API.getRepositorytype, this.params, rs => {
      console.debug(rs);
      this.repositorytype = rs.data;
    })
  }
  //增加按钮
  onSubmit() {
    this.user = JSON.parse(localStorage.getItem('user'));
    this.user = this.user ? JSON.parse(localStorage.getItem('user')) : [];
    console.log(this.user);
    let sessiontoken = this.user.sessiontoken;
    if (sessiontoken === undefined){
      sessiontoken = null;
    }

    this.topicParams.createuser = this.user.user_id;
    this.topicParams.sessiontoken = sessiontoken;

    let succ = 0;
    this.appService.httpPost(AppGlobal.API.saveRepository, this.topicParams, rs => {
      console.debug(rs);
      succ = rs.state;
      console.debug(succ);
      if (succ == 1){
        this.navCtrl.pop();//返回上一个页面
        //this.navCtrl.popToRoot();//返回根页面
      } else {
        this.showflg = true;
        this.errormsg = rs.message;
      }
    });
  }
}
